import React, { useEffect, useState } from 'react';
import SelfHead from 'components/common/selfHead';
import DetailItem from 'components/common/detailItem';
import tranCompDetail from 'dic/tranCompDetail';
import { getCompAddDetail } from 'ser/actions';

import './index.less';

export default function Detail({
  location: {
    query: { id },
  },
}) {
  const [detail, setDetail] = useState({});
  useEffect(() => {
    async function getDetail() {
      const res = await getCompAddDetail(id);
      if (res.code === 0) {
        setDetail(res.rows);
      }
    }
    getDetail();
  }, []);
  const arr = [];
  Object.keys(tranCompDetail).map(key => {
    if (key === 'ownership') {
      if (detail[key] === '01') {
        detail[key] = '一人独资';
      } else {
        detail[key] = '股份制';
      }
    }
    arr.push({
      label: tranCompDetail[key].label,
      value: tranCompDetail[key].unit
        ? detail[key] + tranCompDetail[key].unit
        : detail[key],
    });
  });
  return (
    <div className="detail">
      <SelfHead title="详情" />
      <div className="detail-body">
        {arr.length > 0 &&
          arr.map((item, index) => {
            return item.label ? (
              <DetailItem label={item.label} value={item.value} key={index} />
            ) : null;
          })}
      </div>
    </div>
  );
}
